using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

public class UIManager : Singleton<UIManager>
{
    [SerializeField, Header("炮塔商店面板")]
    private GameObject turretShopPanel;

    private Node _currentNodeSelected; // 当前选中的节点

    [SerializeField] private GameObject nodeUIPanel;
    [SerializeField] private TextMeshProUGUI upgradeText;
    [SerializeField] private TextMeshProUGUI sellText;
    [SerializeField] private TextMeshProUGUI turretLevelText;
    [SerializeField] private TextMeshProUGUI totalCoinsText;
    [SerializeField] private TextMeshProUGUI lifesText;
    [SerializeField] private TextMeshProUGUI currentWaveText;

    [SerializeField] private GameObject gameOverPanel;//游戏结束面板
    [SerializeField] private TextMeshProUGUI gameOverTotalCoinsText;//金币数
    [SerializeField] private TextMeshProUGUI gameOverTotalWaveText;//波数

    private void Update()
    {
        totalCoinsText.text = "金币：" + CurrencySystem.Instance.TotalCoins.ToString();
        lifesText.text = "生命：" + LevelManager.Instance.TotalLives.ToString();
        currentWaveText.text = "波数：" + LevelManager.Instance.CurrentWave.ToString();
    }

    //显示游戏结束面板
    public void ShowGameOverPanel()
    {
        gameOverPanel.SetActive(true);
        gameOverTotalCoinsText.text = "金币：" + CurrencySystem.Instance.TotalCoins.ToString();
        gameOverTotalWaveText.text = "你坚持了 " + LevelManager.Instance.CurrentWave.ToString() + " 波";
    }

    //重新开始
    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void SlowTime()
    {
        Time.timeScale = 0.5f;
    }
    public void ResumeTime()
    {
        Time.timeScale = 1f;
    }
    public void FastTime()
    {
        Time.timeScale = 2f;
    }


    public void CloseNodeUIPanel()
    {
        _currentNodeSelected.CloseAttackRangeSprite();
        nodeUIPanel.SetActive(false);
    }

    public void CloseTurretShopPanel()
    {
        turretShopPanel.SetActive(false);
    }

    private void ShowNodeUI()
    {
        nodeUIPanel.SetActive(true);
        UpdateUpgradeText();
        UpdateTurretLevel();
        UpdateSellValue();
    }

    //升级
    public void UpgradeTurret()
    {
        _currentNodeSelected.Turret.TurretUpgrade.UpgradeTurret();
        UpdateUpgradeText();
        UpdateTurretLevel();
        UpdateSellValue();
    }

    private void UpdateSellValue()
    {
        int sellAmount = _currentNodeSelected.Turret.TurretUpgrade.GetsellValue();
        sellText.text = sellAmount.ToString();
    }

    private void NodeSelected(Node nodeSelected)
    {
        _currentNodeSelected = nodeSelected; // 更新当前选中的节点

        if (_currentNodeSelected.IsEmpty())
        {
            turretShopPanel.SetActive(true); // 显示炮塔商店面板
        }
        else
        {
            ShowNodeUI();
        }
    }

    //出售
    public void SellTurret()
    {
        _currentNodeSelected.SellTurret();
        _currentNodeSelected = null;
        nodeUIPanel.SetActive(false);
    }

    private void UpdateUpgradeText()
    {
        upgradeText.text = _currentNodeSelected.Turret.TurretUpgrade.UpgradeCost.ToString();
    }

    private void UpdateTurretLevel()
    {
        turretLevelText.text = $"等级 {_currentNodeSelected.Turret.TurretUpgrade.Level}";
    }

    private void OnEnable()
    {
        Node.OnNodeSelected += NodeSelected; // 注册节点被选中事件的回调方法
    }

    private void OnDisable()
    {
        Node.OnNodeSelected -= NodeSelected; // 取消注册节点被选中事件的回调方法
    }
}